{{ if is_commonjs}}
const { verify{{if has_form_data_req $.Def }}, objToForm{{end}}{{range calculation_names $.Def}}, {{.}}{{end}} } = require("./functions.js");
{{ else }}
import { verify{{if has_form_data_req $.Def }}, objToForm{{end}}{{range calculation_names $.Def}}, {{.}}{{end}} } from "./functions.js";
{{ end }}

const injure = (instance)=>{
    {{ range $method,$operation := $.Def.Operations}}
    // 路径：{{$.Name}}， 调用方法{{ $method}}
{{ wrap_doc $operation.Summary "    // "}}
{{ wrap_doc $operation.Description "    // "}}{{ if $operation.Parameters}}
    //params要求传输以下属性，没有标注'有默认值'的参数由调用方传输{{ range $key,$par := $operation.Parameters}}
    //-{{ $par.Value.Name}}{{if has_default $par}}(有默认值){{end}}：{{ wrap_doc_skip_first $par.Value.Description "    // "}}{{ end }}{{end}}{{ if $operation.RequestBody}}
    //inputs要求传输以下属性，没有标注'有默认值'的参数由调用方传输{{ range $key,$par := get_body_content $operation}}
    //-{{ $key}}{{if has_default $par}}(有默认值){{end}}：{{ wrap_doc_skip_first $par.Value.Description "    // "}}{{ end }}{{end}}
    {{if input_as_object $.Name $method $operation}}instance.{{ method_name $.Name $method}} = ({{ path_input $operation}}) => {
        {{if has_verifier $operation}}
        var verify_err = null{{ range $key,$par := $operation.Parameters}}{{with verify_options $par }} ||
            verify({{if params_as_object $operation}}params.{{end}}{{ valid_var_name $par.Value.Name }},"{{ $par.Value.Name }}",{{ .ToJSON }}){{ end }}{{ end }}{{ range $key,$par := get_body_content $operation}}{{with verify_options $par }} ||
            verify(input.{{ valid_var_name $key }},"{{ $key }}",{{ .ToJSON }}){{ end }}{{ end }};
        if ( verify_err !=null){
           return verify_err;
        }{{else}}//需要检查参数时，可以在api中标记参数为必填{{ end }}
        {{ if has_default_params $operation}}
        var defaultParam = { {{range $key,$value := param_with_default_value $operation "query"}}
            {{valid_var_name $key}}: {{$value}},{{end }}
        };
        params = Object.assign(defaultParam,params);{{range calculation_to_call $operation }}
        if (params.{{valid_var_name .key}} == undefined){ 
            params.{{valid_var_name .key}}= {{.value}};
        }{{end}}{{end}}
        {{range $key,$value := param_with_default_value $operation "path"}}
        params.{{$key}} = params.{{$key}} || {{$value}};
        {{end}}
        return instance.{{lower_case $method}}(`{{ transform_path_param $.Name $operation}}`{{if not (simple_method $method)}},input{{end}}{{if has_params $operation}},{params}{{end}});
    }{{else}}instance.{{ method_name $.Name $method }} = ({{ expanded_path_input $operation "with_default_value"}}) => {
        {{if has_verifier $operation}}
        var verify_err = null{{ range $key,$par := $operation.Parameters}}{{with verify_options $par }} ||
            verify({{if params_as_object $operation}}params.{{end}}{{ valid_var_name $par.Value.Name }},"{{ $par.Value.Name }}",{{ .ToJSON }}){{ end }}{{ end }}{{ range $key,$par := get_body_content $operation}}{{with verify_options $par }} ||
            verify({{ valid_var_name $key }},"{{ $key }}",{{ .ToJSON }}){{ end }}{{ end }};
        if ( verify_err !=null){
           return verify_err;
        }{{else}}//需要检查参数时，可以在api中标记参数为必填{{ end }}
        {{ if params_as_object $operation}}{{ if has_default_params $operation}}
        var defaultParam = { {{range $key,$value := param_with_default_value $operation "query"}}
            {{valid_var_name $key}}: {{$value}},{{end }}
        };
        params = Object.assign(defaultParam,params);{{range calculation_to_call $operation }}
        if (params.{{valid_var_name .key}} == undefined){ 
            params.{{valid_var_name .key}}= {{.value}};
        }{{end}}{{end}}{{range $key,$value := param_with_default_value $operation "path"}}
        params.{{$key}} = params.{{$key}} || {{$value}};
        {{end}}{{end}}{{if not (simple_method $method)}}{{if body_as_form_data $operation }}var input = new FormData();
        {{ range $key,$par := get_body_content $operation}}objToForm({{$key}}, input,"{{$key}}");
        {{end}}{{else}}var input = {
        {{ range $key,$par := get_body_content $operation}}    {{$key}}: {{$key}},
        {{end}}}{{end}}{{end}}
        return instance.{{lower_case $method}}(`{{ transform_path_param $.Name $operation }}`{{if not (simple_method $method)}},input{{end}}{{if has_params $operation}},{
          {{if params_as_object $operation}}params{{else}}params:{ {{expanded_path_input $operation "just_name" "query"}} }{{end}}
        }{{end}});
    }{{end}}
    {{ end }}
}

{{export_str "default"}} {
    injure
}